package io.renren.modules.paper.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.course.dao.ExamCourseMapper;
import io.renren.modules.course.entity.ExamCourse;
import io.renren.modules.paper.dao.PaperMapper;
import io.renren.modules.paper.dao.PaperQuestionMapper;
import io.renren.modules.paper.entity.*;
import io.renren.modules.paper.service.PaperService;
import io.renren.modules.paper.util.FindContentWithImage;
import io.renren.modules.title.dao.ExamTitleMapper;
import io.renren.modules.title.entity.ExamTitle;
import io.renren.modules.title.entity.ExamTitleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Yang
 * @Date 2019/9/9 15:59
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Autowired
    PaperMapper paperMapper;

    @Autowired
    PaperQuestionMapper paperQueMapper;

    @Autowired
    ExamCourseMapper examCourseMapper;

    @Autowired
    ExamTitleMapper examTitleMapper;

    @Override
    public long countByExample(PaperExample example) {
        return baseMapper.countByExample(example);
    }
    @Override
    public int deleteByExample(PaperExample example) {
        return baseMapper.deleteByExample(example);
    }
    @Override
    public List<Paper> selectByExample(PaperExample example) {
        return baseMapper.selectByExample(example);
    }
    @Override
    public int updateByExampleSelective(Paper record, PaperExample example) {
        return baseMapper.updateByExampleSelective(record,example);
    }
    @Override
    public int updateByExample(Paper record, PaperExample example) {
        return baseMapper.updateByExample(record,example);
    }

    @Override
    public int deletePaper(Integer paperId) {
        //  删除paper_que表中数据
        PaperQuestionExample paperQueExample = new PaperQuestionExample();
        PaperQuestionExample.Criteria criteria = paperQueExample.createCriteria();
        criteria.andPaperIdEqualTo(paperId);
        int deletePaperQueResult = paperQueMapper.deleteByExample(paperQueExample);
        //  删除paper表中数据
        int deletePaperResult = paperMapper.deleteByPrimaryKey(paperId);
        return deletePaperResult;
    }

    @Override
    public List<Map<String, Object>> searchPapersList(String paperName, Integer courseId, Integer paperType) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        if(!paperName.equals("undefined")) {
            criteria.andPaperNameLike("%" + paperName + "%");
        }
        if(courseId != 0){
            criteria.andCourseIdEqualTo(courseId);
        }
        if(paperType != 0){
            criteria.andPaperTypeEqualTo(paperType);
        }
        paperExample.setOrderByClause("paper_id asc");
        List<Paper> paperList = paperMapper.selectByExample(paperExample);
        for (Paper paper : paperList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paperList.indexOf(paper) + 1);
            map.put("paperId", paper.getPaperId());
            map.put("paperName", paper.getPaperName());

            map.put("courseId", paper.getCourseId());
            ExamCourse programingLanguage = examCourseMapper.selectByPrimaryKey(paper.getCourseId());
            map.put("name", programingLanguage.getName());

            map.put("paperDuration", paper.getPaperDuration());
            map.put("paperDifficulty", paper.getPaperDifficulty());
            map.put("paperType", paper.getPaperType());
            map.put("paperAttention", paper.getPaperAttention());
            map.put("singleScore", paper.getSingleScore());
            map.put("multipleScore", paper.getMultipleScore());
            map.put("judgeScore", paper.getJudgeScore());
            map.put("fillScore", paper.getFillScore());

//            Map<String, Integer> queNum = studentHomeService.getPaperQueNumByPaperId(paper.getPaperId());
//            int totalScore = queNum.get("singleNum")*paper.getSingleScore()
//                    + queNum.get("multipleNum")*paper.getMultipleScore()
//                    + queNum.get("judgeNum")*paper.getJudgeScore()
//                    + queNum.get("fillNum")*paper.getFillScore();
//            map.put("totalScore", totalScore);
//            map.put("singleNum", queNum.get("singleNum"));
//            map.put("multipleNum", queNum.get("multipleNum"));
//            map.put("judgeNum", queNum.get("judgeNum"));
//            map.put("fillNum", queNum.get("fillNum"));
//            map.put("totalNum", queNum.get("totalNum"));

            map.put("participateNum", paper.getParticipateNum());
            map.put("paperCreateTime", paper.getPaperCreateTime());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    @Transactional
    public int fixedInsertPaperInfo(PaperData temp) {
//        String paperName = (String) temp.get("paperName");
//        Integer paperDuration = (Integer) temp.get("paperDuration");
//        Integer paperDifficulty = (Integer) temp.get("paperDifficulty");
//        String paperAttention = (String) temp.get("paperAttention");
//        Integer singleScore = (Integer) temp.get("singleScore");
//        List<Integer> singleNum = (List<Integer>) temp.get("singleNum");
//        Integer multipleScore = (Integer) temp.get("multipleScore");
//        List<Integer> multipleNum = (List<Integer>) temp.get("multipleNum");
//        Integer judgeScore = (Integer) temp.get("judgeScore");
//        List<Integer> judgeNum = (List<Integer>) temp.get("judgeNum");
//        Integer fillScore = (Integer) temp.get("fillScore");
//        List<Integer> fillNum = (List<Integer>) temp.get("fillNum");
//        Integer courseId = (Integer) temp.get("courseId");
        String paperName = temp.getPaperName();
        Integer paperDuration = temp.getPaperDuration();
        Integer paperDifficulty = temp.getPaperDifficulty();
        String paperAttention = temp.getPaperAttention();
        Integer singleScore = temp.getSingleScore();
        List<Integer> singleNum = temp.getSingleNum();
        Integer multipleScore = temp.getMultipleScore();
        List<Integer> multipleNum = temp.getMultipleNum();
        Integer judgeScore = temp.getJudgeScore();
        List<Integer> judgeNum = temp.getJudgeNum();
        Integer fillScore = temp.getFillScore();
        List<Integer> fillNum = temp.getFillNum();
        Integer courseId = temp.getCourseId();
        Paper paper = new Paper();
        paper.setPaperName(paperName);
        paper.setPaperDuration(paperDuration);
        paper.setPaperDifficulty(paperDifficulty);
        paper.setPaperAttention(paperAttention);
        paper.setSingleScore(singleScore);
        paper.setMultipleScore(multipleScore);
        paper.setJudgeScore(judgeScore);
        paper.setFillScore(fillScore);
        paper.setCourseId(courseId);
        paper.setPaperCreateTime(new Date());
        paper.setPaperType(2);
        int paperId = paperMapper.selectMaxPaperId() + 1;
        paper.setPaperId(paperId);

        //  插入试卷信息到paper表
        int result = paperMapper.insertSelective(paper);

        //单选题
        if (singleNum != null) {
            for (Integer singleId : singleNum) {
                PaperQuestion paperQue = new PaperQuestion();
                paperQue.setQueType(0);
//            paperQue.setSingleId(singleId);
                paperQue.setQueId(singleId);
                paperQue.setPaperId(paperId);
                paperQue.setCreateTime(new Date());
                paperQueMapper.insertSelective(paperQue);
                //更新compose_flag字段
                ExamTitle examTitle = new ExamTitle();
                examTitle.setId(singleId);
                examTitle.setComposeFlag("1");
                examTitleMapper.updateById(examTitle);
            }
        }
        //多选题
        if (multipleNum != null) {
            for (Integer multipleId : multipleNum) {
                PaperQuestion paperQue = new PaperQuestion();
                paperQue.setQueType(1);
//            paperQue.setMultipleId(multipleId);
                paperQue.setPaperId(paperId);
                paperQue.setQueId(multipleId);
                paperQue.setCreateTime(new Date());
                paperQueMapper.insertSelective(paperQue);
                //更新compose_flag字段
                ExamTitle examTitle = new ExamTitle();
                examTitle.setId(multipleId);
                examTitle.setComposeFlag("1");
                examTitleMapper.updateById(examTitle);
            }
        }
        //判断题
        if (judgeNum != null) {
            for (Integer judgeId : judgeNum) {
                PaperQuestion paperQue = new PaperQuestion();
                paperQue.setQueType(2);
//            paperQue.setFillId(judgeId);
                paperQue.setPaperId(paperId);
                paperQue.setQueId(judgeId);
                paperQue.setCreateTime(new Date());
                paperQueMapper.insertSelective(paperQue);
                //更新compose_flag字段
                ExamTitle examTitle = new ExamTitle();
                examTitle.setId(judgeId);
                examTitle.setComposeFlag("1");
                examTitleMapper.updateById(examTitle);
            }
        }
        //填空题
        if (fillNum != null) {
            for (Integer fillId : fillNum) {
                PaperQuestion paperQue = new PaperQuestion();
                paperQue.setQueType(3);
//            paperQue.setFillId(fillId);
                paperQue.setPaperId(paperId);
                paperQue.setQueId(fillId);
                paperQue.setCreateTime(new Date());
                paperQueMapper.insertSelective(paperQue);
                //更新compose_flag字段
                ExamTitle examTitle = new ExamTitle();
                examTitle.setId(fillId);
                examTitle.setComposeFlag("1");
                examTitleMapper.updateById(examTitle);
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> getPaperQueDetailByCId(Integer cid) {
        Map<String, Object> map = new HashMap<>();

        List<Map<Object, Object>> singleData = new ArrayList<>();
        List<Map<String, Object>> multipleData = new ArrayList<>();
        List<Map<String, Object>> judgeData = new ArrayList<>();
        List<Map<String, Object>> fillData = new ArrayList<>();

        ExamTitleExample examTitleExample = new ExamTitleExample();
        ExamTitleExample.Criteria criteria = examTitleExample.createCriteria();
        criteria.andCidEqualTo(cid);
        List<ExamTitle> exampleList = examTitleMapper.selectByExample(examTitleExample);

        for (ExamTitle examTitle : exampleList) {
            if (examTitle.getType() == 0) {
                int questionId = examTitle.getId();
                Map<Object, Object> singleMap = new HashMap<>();
                singleMap.put("id", questionId);
                //获取单选题题目内容
                Map<String, String> singleContentMap = FindContentWithImage.findContentWithImage(examTitle.getQuestion());
                String composeFlag = examTitle.getComposeFlag();
                String composeStr = composeFlag.equals("1") ? "（已组过）" : "（未组过）";
                singleMap.put("label", composeStr + (exampleList.indexOf(examTitle) + 1) + "、" + examTitle.getQuestion());
                singleData.add(singleMap);
            }
        }
        for (ExamTitle examTitle : exampleList) {
            if (examTitle.getType() == 1) {
                int questionId = examTitle.getId();
                Map<String, Object> multipleMap = new HashMap<>();
                // 在每个multipleId前加上2连接
                multipleMap.put("id", questionId);
                Map<String, String> multipleContentMap = FindContentWithImage.findContentWithImage(examTitle.getQuestion());
                String composeFlag = examTitle.getComposeFlag();
                String composeStr = composeFlag.equals("1") ? "（已组过）" : "（未组过）";
                multipleMap.put("label", composeStr + (exampleList.indexOf(examTitle) + 1) + "、" + examTitle.getQuestion());
                multipleData.add(multipleMap);
            }
        }
        for (ExamTitle examTitle : exampleList) {
            if (examTitle.getType() == 2) {
                int questionId = examTitle.getId();
                Map<String, Object> judgeMap = new HashMap<>();
                // 在每个judgeId前加上3连接
                judgeMap.put("id", questionId);
                String composeFlag = examTitle.getComposeFlag();
                String composeStr = composeFlag.equals("1") ? "（已组过）" : "（未组过）";
                judgeMap.put("label", composeStr + (exampleList.indexOf(examTitle) + 1) + "、" + examTitle.getQuestion());
                judgeData.add(judgeMap);
            }
        }
        for (ExamTitle examTitle : exampleList) {
            if (examTitle.getType() == 3) {
                int questionId = examTitle.getId();
                Map<String, Object> fillMap = new HashMap<>();
                // 在每个fillId前加上4连接
                fillMap.put("id", questionId);
                //获取单选题题目内容
                String composeFlag = examTitle.getComposeFlag();
                String composeStr = composeFlag.equals("1") ? "（已组过）" : "（未组过）";
                fillMap.put("label", composeStr + (exampleList.indexOf(examTitle) + 1) + "、" + examTitle.getQuestion());
                fillData.add(fillMap);
            }
        }
        map.put("singleData",singleData);
        map.put("multipleData",multipleData);
        map.put("judgeData",judgeData);
        map.put("fillData",fillData);
        return map;
    }

    @Override
    public int randomInsertPaperInfo(PaperTitle temp) {
//        String paperName = (String) temp.get("paperName");
//        Integer paperDuration = (Integer) temp.get("paperDuration");
//        Integer paperDifficulty = (Integer) temp.get("paperDifficulty");
//        String paperAttention = (String) temp.get("paperAttention");
//        Integer singleScore = (Integer) temp.get("singleScore");
//        Integer singleNum = (Integer) temp.get("singleNum");
//        Integer multipleScore = (Integer) temp.get("multipleScore");
//        Integer multipleNum = (Integer) temp.get("multipleNum");
//        Integer judgeScore = (Integer) temp.get("judgeScore");
//        Integer judgeNum = (Integer) temp.get("judgeNum");
//        Integer fillScore = (Integer) temp.get("fillScore");
//        Integer fillNum = (Integer) temp.get("fillNum");
//        Integer cid = (Integer) temp.get("cid");
        String paperName = temp.getPaperName();
        Integer paperDuration = temp.getPaperDuration();
        Integer paperDifficulty = temp.getPaperDifficulty();
        String paperAttention = temp.getPaperAttention();
        Integer singleScore = temp.getSingleScore();
        Integer singleNum = temp.getSingleNum();
        Integer multipleScore = temp.getMultipleScore();
        Integer multipleNum = temp.getMultipleNum();
        Integer judgeScore = temp.getJudgeScore();
        Integer judgeNum = temp.getJudgeNum();
        Integer fillScore = temp.getFillScore();
        Integer fillNum = temp.getFillNum();
        Integer cid = temp.getCourseId();
        Paper paper = new Paper();
        paper.setPaperName(paperName);
        paper.setPaperDuration(paperDuration);
        paper.setPaperDifficulty(paperDifficulty);
        paper.setPaperAttention(paperAttention);
        paper.setSingleScore(singleScore);
        paper.setMultipleScore(multipleScore);
        paper.setJudgeScore(judgeScore);
        paper.setFillScore(fillScore);
        paper.setCourseId(cid);
        paper.setPaperCreateTime(new Date());
        paper.setPaperType(1);
        int paperId = paperMapper.selectMaxPaperId() + 1;
        paper.setPaperId(paperId);

        //  插入试卷信息到paper表
        int result = paperMapper.insertSelective(paper);

        //  插入试卷问题信息到paper_que表
        //  单选题
        List<ExamTitle> examTitleList = examTitleMapper.getRandomSingleByCountAndCId(cid, singleNum);
        for (ExamTitle examTitle : examTitleList) {
            int singleId = examTitle.getId();
            PaperQuestion paperQue = new PaperQuestion();
            paperQue.setQueType(1);
            paperQue.setQueId(singleId);
            paperQue.setPaperId(paperId);
            paperQue.setCreateTime(new Date());
            paperQueMapper.insertSelective(paperQue);
//            //更新compose_flag字段
//            examTitle.setComposeFlag("1");
//            examTitleMapper.updateByPrimaryKeySelective(examTitle);
        }

        //  多选题
        List<ExamTitle> bankMultipleChoiceQueList = examTitleMapper.getRandomMultipleByCountAndCId(cid, multipleNum);
        for (ExamTitle examTitle : bankMultipleChoiceQueList) {
            int multipleId = examTitle.getId();
            PaperQuestion paperQue = new PaperQuestion();
            paperQue.setQueType(2);
            paperQue.setQueId(multipleId);
            paperQue.setPaperId(paperId);
            paperQue.setCreateTime(new Date());
            paperQueMapper.insertSelective(paperQue);
//            //更新compose_flag字段
//            examTitle.setComposeFlag("1");
//            examTitleMapper.updateByPrimaryKeySelective(examTitle);
        }

        //  判断题
        List<ExamTitle> bankJudgeQueList = examTitleMapper.getRandomJudgeByCountAndCId(cid, judgeNum);
        for (ExamTitle examTitle : bankJudgeQueList) {
            int judgeId = examTitle.getId();
            PaperQuestion paperQue = new PaperQuestion();
            paperQue.setQueType(3);
            paperQue.setQueId(judgeId);
            paperQue.setPaperId(paperId);
            paperQue.setCreateTime(new Date());
            paperQueMapper.insertSelective(paperQue);
//            //更新compose_flag字段
//            examTitle.setComposeFlag("1");
//            examTitleMapper.updateByPrimaryKeySelective(examTitle);
        }

        //  填空题
        List<ExamTitle> bankFillQueList = examTitleMapper.getRandomFillByCountAndCId(cid, fillNum);
        for (ExamTitle examTitle : bankFillQueList) {
            int fillId = examTitle.getId();
            PaperQuestion paperQue = new PaperQuestion();
            paperQue.setQueType(4);
            paperQue.setQueId(fillId);
            paperQue.setPaperId(paperId);
            paperQue.setCreateTime(new Date());
            paperQueMapper.insertSelective(paperQue);
//            //更新compose_flag字段
//            examTitle.setComposeFlag("1");
//            examTitleMapper.updateByPrimaryKeySelective(examTitle);
        }

        return result;
    }

}
